﻿using Assets.Core.Common.Web;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Nucleic.Core.DoMain;
using Nucleic.Core.Utils;

namespace Nucleic.Web.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class TimeRangeController : ControllerBase
    {
        /// <summary>
        /// 获取可用的时段
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [Route("GetAll")]
        public Result<List<string>> GetAll( )
        {
            var timeRange = SqlSugarFactory.Instance.Queryable<time_range>()
                 .Where(it => it.status == "0").Select(it=>it.range).Distinct().ToList();
            return Result<List<string>>.success(timeRange);
        }
        /// <summary>
        /// 获取可用时段+允许最大预约人数
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [Route("GetAllWithMaxNum")]
        public Result<List<time_range>> GetAllWithMaxNum()
        {
            var timeRange = SqlSugarFactory.Instance.Ado.SqlQuery<time_range>("select distinct range,max_num from time_range(nolock)")
                 .ToList();
            return Result<List<time_range>>.success(timeRange);
        }
        /// <summary>
        /// 根据类别id查询时段
        /// </summary>
        /// <param name="type_id">几比几的检测类型</param>
        /// <returns></returns>
        [HttpPost]
        [Route("QueryByCheckType")]
        public Result<List<time_range>> QueryByCheckType(int type_id)
        {
            var timeRange = SqlSugarFactory.Instance.Queryable<time_range>()
                 .Where(it => it.type_id == type_id && it.status == "0").ToList();
            return Result<List<time_range>>.success(timeRange);
        }
        /// <summary>
        /// 查询时段内预约的人数
        /// </summary>
        /// <param name="range"></param>
        /// <param name="dateTime"></param>
        /// <returns></returns>
        [HttpPost]
        [Route("QueryBookCount")]
        public Result QueryBookCount(string range, DateTime dateTime)
        {
            var randids = SqlSugarFactory.Instance.Queryable<time_range>()
                .Where(it => it.range == range && it.status == "0").Select(it => it.range_id).ToList();
            var c = SqlSugarFactory.Instance.Queryable<book_info>()
                 .Where(it => randids.Contains(it.range_id) && it.appo_date.Date == dateTime.Date).Count();
            return Result.success(c);
        }
    }
}
